var express = require("express");
var router = express.Router();

/* GET users listing. */
router.get("/ping", (req, res, next) => {
  res.send({ msg: "connect success" });
});

// 模拟一个 token
const token = "019ccf86db86e07bbff5be2df9d83e3d11795500";

// 获取推荐数据
router.post("/recoms", (req, res) => {
  console.log(req.body);
  if (req.body.token !== token) {
    return res.send({ code: 1 });
  }

  // 模拟接口数据
  const data = [
    {
      id: 101,
      product_name: "苹果"
    },
    {
      id: 102,
      product_name: "香蕉"
    },
    {
      id: 103,
      product_name: "梨子"
    }
  ];

  res.send({ code: 0, data });
});

// 模拟登录数据
router.post("/login", function(req, res) {
  const { username, password } = req.body;
  if (username === "demo" && password === "demo") {
    return res.send({
      code: 0,
      data: {
        uid: 1001,
        nickname: "user1",
        avatar:
          "http://p1.music.126.net/r-5cPAKAeeG9pcdWNJTy2Q==/109951164962408101.jpg",
        passer: 0,
        sign: "TA什么都没留下",
        token: token
      }
    });
  }

  return res.send({
    code: 1
  });
});

module.exports = router;
